﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BrainTechLLC;
using BrainTechLLC.Silverlight.Controls;
using System.Threading;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class DownloadData
    {
        public event EventHandler<ObjectEventArgs<Type, ByteDataMessage>> RequestComplete;

        protected Type _thingType;

        public DownloadData()
        {
            InitializeComponent();
        }

        private void typeFilter_TypeChanged(object sender, ObjectEventArgs<Type> e)
        {
            _thingType = e.obj;
        }

        private void btnDownload_Click(object sender, RoutedEventArgs e)
        {
            if (_thingType == null)
            {
                MessageBox.Show("Select a type of thing to download.");
            }
            else
            {
                DataAccess.RequestComplete += new EventHandler<ObjectEventArgs<Type, ByteDataMessage>>(DataAccess_RequestComplete);
                DataAccess.DownloadData(_thingType);
            }
        }

        void DataAccess_RequestComplete(object sender, ObjectEventArgs<Type, ByteDataMessage> e)
        {
            DataAccess.RequestComplete -= DataAccess_RequestComplete;

            if (RequestComplete != null)
                RequestComplete(this, e);
        }        
    }
}
